package com.example.myapplication.bean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashSet;
import java.util.Set;

public class MessageReceived {
    //private String message;
    private JSONObject json;
    private String username;
    public MessageReceived(String message, String username) throws JSONException {
        json = new JSONObject(message);
        this.username = username;

    }
    //消息内容
    public String getTextMessage() throws JSONException {
        return json.getString("textMessage");
    }
    //发送人
    public String getUsername() throws JSONException {
        return json.getString("username");
    }
    //消息类型：1.上线2.下线3.在线4.聊天消息
    public int getMessageType() throws JSONException {
        return json.getInt("messageType");
    }

    public int getNumber() throws JSONException {
        return json.getInt("number");
    }

    public Set<String> getOnlineUsers() throws JSONException {
        JSONArray jsonArray = json.getJSONArray("onlineUsers");

        Set<String> stringSet = new HashSet<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            String value = jsonArray.getString(i);
            stringSet.add(value);
        }
        return stringSet;
    }
    public boolean isSentByMyself() throws JSONException {
        return getUsername().equals(username);
    }

    public String getRPicture() throws JSONException{
        return json.getString("picture");
    }

    public String getPicture() throws JSONException {
        return json.getString("picture");
    }
}
